//
//  ProfileViewController.m
//  ContactManager
//
//  Created by mzhou19 on 11/2/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ProfileViewController.h"


@implementation ProfileViewController
@synthesize message;
@synthesize name,phoneNumber,address,email,imageOfContact;
@synthesize deleteProfile;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    self.title = @"Profile";
    [super viewDidLoad];
//    self.name.titleLabel.text = [message valueForKey:@"name"];
//    self.phoneNumber.titleLabel.text = [message valueForKey:@"phone number"];
//    self.address.titleLabel.text = [message valueForKey:@"home address"];
//    self.email.titleLabel.text = [message valueForKey:@"email"];
    [self.name setTitle:[message valueForKey:@"name"] forState:UIControlStateNormal];
    [self.phoneNumber setTitle:[message valueForKey:@"phone number"] forState:UIControlStateNormal];
    [self.address setTitle:[message valueForKey:@"home address"] forState:UIControlStateNormal];
    [self.email setTitle:[message valueForKey:@"email"] forState:UIControlStateNormal];
    NSString *image=[message valueForKey:@"image"];
    self.imageOfContact.image=[UIImage imageNamed:image];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit 
                                                                          target:self 
                                                                          action:@selector(editProfile)];
	[[self navigationItem] setRightBarButtonItem:item];
	[item release];
    // Do any additional setup after loading the view from its nib.
}
- (void)editProfile {
    
	ProfileEditingViewController *editViewController = 
	[[[ProfileEditingViewController alloc] initWithNibName:@"ProfileEditingViewController" 
										  bundle:nil] autorelease];
	
	editViewController.message = message;
	
	[self.navigationController pushViewController:editViewController 
										 animated:YES];
    
	//[[self tableView] reloadData];
}

- (IBAction)emailButtonPressed
{
    MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
    composer.mailComposeDelegate = self;
    if([MFMailComposeViewController canSendMail])
    {
        [composer setToRecipients:[NSArray arrayWithObjects:[message valueForKey:@"email"],nil]];
        //[composer setToRecipients:[message valueForKey:@"email"]];
        [composer setSubject:@"Subject"];
        [composer setMessageBody:@"This is the body" isHTML:NO];
        [composer setMessageBody:@"Hey,\n\nChect out this awesome image!\n\n" isHTML:NO];
        [composer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
        UIImage *lion = [UIImage imageNamed:@"mac-lion.jpg"];
        NSData *lionData = UIImageJPEGRepresentation(lion, 1);
        [composer addAttachmentData:lionData mimeType:@"image/jpeg" fileName:@"mac-lion.jpg"];
        
        [self presentModalViewController:composer animated:YES];
        [composer release];
    }

}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    [self dismissModalViewControllerAnimated:YES];
    if(result == MFMailComposeResultFailed) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failed!" message:@"The email failed to send" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitle:nil];
        [alert show];
        [alert release];
        [self dismissModalViewControllerAnimated:YES];
    }
}

- (IBAction)addressButtonPressed
{
    LatitudeSelectViewController *editViewController = 
	[[[LatitudeSelectViewController alloc] initWithNibName:@"LatitudeSelectViewController" 
                                                    bundle:nil] autorelease];
	
	//editViewController.message = message;
	
	[self.navigationController pushViewController:editViewController 
										 animated:YES];
    
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)deleteButtonPressed{
    // open a alert with an OK and cancel button
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert!" message:@"Are you sure you want to delete this contact profile?"
                                                   delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Yes", nil];
    [alert show];
    [alert release];
}

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
    if ([buttonTitle isEqualToString:@"Yes"]) {
        [[RecordManager sharedManager] removeRecord:message];
        [self.navigationController popViewControllerAnimated:YES];
    }
}
@end
